﻿/**
 *  smsx.cab
 * @author Leo
 * @Date 2013-08-28
 * 
 * 备注：
 * 【页眉、页脚】显示：居左显示&b居中显示&b居右显示页码，第&p页/共&P页
 */

/**
 * 初始化设置
 */
function appendObject() {
    var obj = document.createElement("object");
    obj.setAttribute("id", "WebBrowser");
    obj.setAttribute("classid", "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2");
    obj.style.display = "none";
    document.body.appendChild(obj);
}
function p_init(printObj) {
    if (checkIE11() == -2) {
        appendObject();
        return;
    }
    printObj = printObj ? printObj : {};
    var header = (printObj.header != null) ? printObj.header : "";
    var footer = (printObj.footer != null) ? printObj.footer : "&b第&p页/共&P页&b";
    var leftMargin = printObj.leftMargin ? printObj.leftMargin : 10;//10mm
    var topMargin = printObj.topMargin ? printObj.topMargin : 10;
    var rightMargin = printObj.rightMargin ? printObj.rightMargin : 10;
    var bottomMargin = printObj.bottomMargin ? printObj.bottomMargin : 10;
	/*var portrait=true;//true纵向,false横向
	if(printObj.portrait==false){
		portrait=false;
	}*/

    try {
        if (factory && factory.printing) {
            factory.printing.header = header;//页眉
            factory.printing.footer = footer;//页脚
            if (!myIsEmpty(printObj.portrait)) {
                var portrait = true;//true纵向,false横向
                if (printObj.portrait == false) {
                    portrait = false;
                }
                factory.printing.portrait = portrait;//true纵向,false横向
            }
        }
        //		factory.printing.leftMargin = leftMargin;//左边距
        //		factory.printing.topMargin = topMargin;//上边距 
        //		factory.printing.rightMargin = rightMargin;//右边距
        //		factory.printing.bottomMargin = bottomMargin;//下边距
    } catch (e) {
        if (e.description == 'Error querying the default printer') {
            alert("找不到默认打印机，请添加一个默认打印机！");
        } else {
            throw e;
        }
    }
}
var myIsEmpty = function (obj) {
    if (obj == null) {
        return true;
    }
    if (typeof (obj) == 'string' && jQuery.trim(obj) == '') {
        return true;
    }
    if (typeof (obj) == 'object') {
        return jQuery.isEmptyObject(obj);
    }
    return false;
};

/**
 * 打印预览
 */
function p_preview() {
    try {
        factory.printing.Preview();
    } catch (e) {
        if (e.description == 'Error querying the default printer') {
            alert("找不到默认打印机，请添加一个默认打印机！");
        } else {
            //throw e;
            document.all.WebBrowser.ExecWB(7, 1);
        }
    }
}

/**
 * 打印设置
 */
function p_pagesetup() {
    try {
        factory.printing.PageSetup();
    } catch (e) {
        if (e.description == 'Error querying the default printer') {
            alert("找不到默认打印机，请添加一个默认打印机！");
        } else {
            document.all.WebBrowser.ExecWB(8, 1);
            //throw e;
        }
    }
}

/**
 * 打印
 */
function p_print() {
    try {
        factory.printing.Print();
    } catch (e) {
        if (e.description == 'Error querying the default printer') {
            alert("找不到默认打印机，请添加一个默认打印机！");
        } else {
            window.print();
            //throw e;
        }
    }
}
var ClientUtil = {};
ClientUtil.getScreen = function () {
    return window.screen;
};
ClientUtil.isIE = (!!window.ActiveXObject || "ActiveXObject" in window) ? true : false;
ClientUtil.isFF = !ClientUtil.isIE;
ClientUtil.getIEVersion = function () {
    if (ClientUtil.isIE) {
        var userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.match(/msie ([\d.]+)/) != null) {//ie6--ie9                
            uaMatch = userAgent.match(/msie ([\d.]+)/);
            return parseInt(uaMatch[1]);
        } else if (userAgent.match(/(trident)\/([\w.]+)/)) {
            uaMatch = userAgent.match(/trident\/([\w.]+)/);
            switch (uaMatch[1]) {
                case "4.0": return 8; break;
                case "5.0": return 9; break;
                case "6.0": return 10; break;
                case "7.0": return 11; break;
                default: return null;
            }
        }
        return null;
    }
    return null;
};
var ieVersion = (ClientUtil.getIEVersion());
if (!ClientUtil.isIE) {
    alert("请您使用 Internet Explorer 浏览器!");
} else {

}
function checkIE11(msg) {
    if (!ClientUtil.isIE) {
        if (msg) {
            alert(msg);
        }
        //alert("该功能当前仅适用于 Internet Explorer 浏览器");
        return -1;
    } else {
        if (ieVersion == 11) {
            return -2;
        } else {
            return 0;
        }
    }
}
function createHelp() {
    var f = document.getElementById("factory");
    var codeBase = f.getAttribute("codebase");
    var path = codeBase.split("smsx.cab")[0];
    path = path + "smsxw.rar";
    var div = document.createElement("div");
    div.className = "Noprint";
    div.style.backgroundColor = "#ccc";
    div.style.padding = "5px 0px";
    div.style.marginBottom = "5px";
    div.style.textAlign = "center";
    div.innerHTML = "<div>如页面中的打印设置、打印预览、打印按钮无效，请<a target='_blank' href='" + path + "'>[下载]</a>并安装打印软件，安装完成后刷新本页面重试。</div>";
    document.body.insertBefore(div, f);
}